package com.echo.im.platform.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.echo.im.client.facade.IMClient;
import com.echo.im.common.model.message.IMGroupMessage;
import com.echo.im.mq.event.model.GroupStoreMessageEvent;
import com.echo.im.mq.event.producer.EventPublisher;
import com.echo.im.platform.core.common.ResultEnum;
import com.echo.im.platform.core.exception.GlobalException;
import com.echo.im.repository.dao.entity.GroupMember;
import com.echo.im.repository.dao.entity.GroupMessage;
import com.echo.im.repository.dao.mapper.GroupMemberMapper;
import com.echo.im.repository.dao.mapper.GroupMessageMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * @author: echo
 * @time: 2024-08-17
 */
@RequiredArgsConstructor
@Component
public class GroupMessageManager {

    private final IMClient imClient ;
    private final EventPublisher eventPublisher ;
    private final GroupStoreMessageEvent groupStoreMessageEvent;
    private final GroupMessageMapper groupMessageMapper;


    public  GroupMessage getAndCheckByMsgId(Long messageId) {
        GroupMessage message = groupMessageMapper.selectById(messageId);
        if (Objects.isNull(message)) {
            throw new GlobalException(ResultEnum.NO_MESSAGE);
        }
        return message;
    }


    public void sendAndSave(IMGroupMessage imGroupMessage) {
        imClient.sendMessage(imGroupMessage);
        //持久化
        eventPublisher.publish(groupStoreMessageEvent.topic(),groupStoreMessageEvent.buildEventMessage(imGroupMessage.getData()));
    }


}
